package action;

import config.ApplicationConfig;
import net.sf.json.JSONObject;
import server.GameServer;
import server.LoginServer;
import server.SettingServer;
import server.UserServer;
import util.Decode;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;

/**
 * User: tum0r
 * Package Name: ${PACKAGE_NAME}
 * Time: 2019-04-18 Thursday 22:29:29
 */
public class ChangeGameInformationServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        LinkedHashMap<String, String> pageConfig = ApplicationConfig.readConfig("page.json");
        request.setCharacterEncoding(pageConfig.get("encode"));
        response.setCharacterEncoding(pageConfig.get("encode"));

        HttpSession session = request.getSession();
        String username = session.getAttribute("user") == null ? "" : session.getAttribute("user").toString();
        String cookie = session.getAttribute("cookie") == null ? "" : session.getAttribute("cookie").toString();
        String page = "information.jsp";
        if (username == null || username.equals("") || cookie == null || cookie.equals("")) {
            session.setAttribute("infoTitle", "未登录");
            session.setAttribute("infoContent", "您还未登录，请先登录");
            session.setAttribute("return", "index.jsp");
            session.setAttribute("panelColor", "red");
            session.removeAttribute("user");
            session.removeAttribute("cookie");
        } else {
            LoginServer loginServer = new LoginServer();
            loginServer.open();
            if (!loginServer.checkLogin(username, cookie)) {
                String name = request.getParameter("name");
                String sponsor = request.getParameter("sponsor");
                String start = request.getParameter("start");
                String stop = request.getParameter("stop");
                String timeIsUp = request.getParameter("time_is_up");
                String punish = request.getParameter("punish");
                if (name != null && sponsor != null && start != null && stop != null && punish != null && timeIsUp != null) {
                    UserServer userServer = new UserServer();
                    userServer.open();
                    int canChange = userServer.canChange(username, "");
                    userServer.close();

                    switch (canChange) {
                        case 2:

                            GameServer gameServer = new GameServer();
                            gameServer.open();
                            JSONObject game = gameServer.getGameInformation();
                            gameServer.close();
                            game.replace("name", name);
                            game.replace("sponsor", sponsor);
                            game.replace("start", start);
                            game.replace("stop", stop);
                            game.replace("time_is_up", timeIsUp);
                            game.replace("punish", punish);


                            game.put("start_audio", false);
                            game.put("time_is_up_audio", false);
                            game.put("stop_audio", false);

                            SimpleDateFormat gameTime = new SimpleDateFormat("yyyy/MM/dd HH:mm");
                            SimpleDateFormat punishedTime = new SimpleDateFormat("HH:mm");
                            try {
                                long startTime = gameTime.parse(start).getTime();
                                long stopTime = gameTime.parse(stop).getTime();
                                punishedTime.parse(punish);
                                punishedTime.parse(timeIsUp);
                                if (startTime < stopTime) {
                                    long now = new Date().getTime();
                                    SettingServer settingServer = new SettingServer();
                                    settingServer.open();
                                    settingServer.recordSettingObject("game", game);
                                    settingServer.close();
                                    session.setAttribute("infoTitle", "更改比赛信息");
                                    session.setAttribute("infoContent", "更改成功");
                                    session.setAttribute("return", "mine");
                                    session.setAttribute("panelColor", "blue");
                                } else {
                                    session.setAttribute("infoTitle", "更改比赛信息");
                                    session.setAttribute("infoContent", "更改失败，比赛开始时间要早于结束时间");
                                    session.setAttribute("return", "mine");
                                    session.setAttribute("panelColor", "red");
                                }
                            } catch (Exception e) {
                                session.setAttribute("infoTitle", "更改比赛信息");
                                session.setAttribute("infoContent", "更改失败，比赛开始和结束时间格式为<br>yyyy/MM/dd HH:mm<br>例如2019/03/15 06:00<br>n加罚时长格式为<br>HH:M<br>例如00:20");
                                session.setAttribute("return", "mine");
                                session.setAttribute("panelColor", "red");
                            }
                            break;
                        default:
                            session.setAttribute("infoTitle", "更改比赛信息");
                            session.setAttribute("infoContent", "更改失败，你没有权限修改");
                            session.setAttribute("return", "mine");
                            session.setAttribute("panelColor", "red");
                            break;
                    }
                }
            } else {
                session.setAttribute("infoTitle", "未登录");
                session.setAttribute("infoContent", "您还未登录，请先登录");
                session.setAttribute("return", "index.jsp");
                session.setAttribute("panelColor", "red");
                session.removeAttribute("user");
                session.removeAttribute("cookie");
            }
            loginServer.close();
        }
        response.sendRedirect(page);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
